package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsWFyDetailBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsWFyDetailVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsWFyDetail;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsWFyDetailMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsWFyDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 万能机详情信息(3标)Service业务层处理
 *
 * @author tll
 * @date 2023-09-15
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsWFyDetailServiceImpl implements INLaboratoryMechanicsWFyDetailService {

    private final NLaboratoryMechanicsWFyDetailMapper baseMapper;

    /**
     * 查询万能机详情信息(3标)
     */
    @Override
    public NLaboratoryMechanicsWFyDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询万能机详情信息(3标)列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsWFyDetailVo> queryPageList(NLaboratoryMechanicsWFyDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsWFyDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsWFyDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询万能机详情信息(3标)列表
     */
    @Override
    public List<NLaboratoryMechanicsWFyDetailVo> queryList(NLaboratoryMechanicsWFyDetailBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsWFyDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsWFyDetail> buildQueryWrapper(NLaboratoryMechanicsWFyDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsWFyDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGroupId() != null, NLaboratoryMechanicsWFyDetail::getGroupId, bo.getGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getSYBH()), NLaboratoryMechanicsWFyDetail::getSYBH, bo.getSYBH());
        lqw.eq(StringUtils.isNotBlank(bo.getLQ()), NLaboratoryMechanicsWFyDetail::getLQ, bo.getLQ());
        lqw.eq(StringUtils.isNotBlank(bo.getXH()), NLaboratoryMechanicsWFyDetail::getXH, bo.getXH());
        lqw.eq(StringUtils.isNotBlank(bo.getSYSL()), NLaboratoryMechanicsWFyDetail::getSYSL, bo.getSYSL());
        lqw.eq(StringUtils.isNotBlank(bo.getTag()), NLaboratoryMechanicsWFyDetail::getTag, bo.getTag());
        lqw.eq(StringUtils.isNotBlank(bo.getSYLX()), NLaboratoryMechanicsWFyDetail::getSYLX, bo.getSYLX());
        lqw.eq(bo.getSYSJ() != null, NLaboratoryMechanicsWFyDetail::getSYSJ, bo.getSYSJ());
        lqw.eq(StringUtils.isNotBlank(bo.getCXSJ()), NLaboratoryMechanicsWFyDetail::getCXSJ, bo.getCXSJ());
        lqw.eq(StringUtils.isNotBlank(bo.getZDSL()), NLaboratoryMechanicsWFyDetail::getZDSL, bo.getZDSL());
        lqw.eq(StringUtils.isNotBlank(bo.getSBLC()), NLaboratoryMechanicsWFyDetail::getSBLC, bo.getSBLC());
        lqw.eq(StringUtils.isNotBlank(bo.getLZDW()), NLaboratoryMechanicsWFyDetail::getLZDW, bo.getLZDW());
        lqw.eq(StringUtils.isNotBlank(bo.getZDL()), NLaboratoryMechanicsWFyDetail::getZDL, bo.getZDL());
        lqw.eq(StringUtils.isNotBlank(bo.getQFD()), NLaboratoryMechanicsWFyDetail::getQFD, bo.getQFD());
        lqw.eq(StringUtils.isNotBlank(bo.getRp02()), NLaboratoryMechanicsWFyDetail::getRp02, bo.getRp02());
        lqw.eq(StringUtils.isNotBlank(bo.getYSGCSJ()), NLaboratoryMechanicsWFyDetail::getYSGCSJ, bo.getYSGCSJ());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NLaboratoryMechanicsWFyDetail::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增万能机详情信息(3标)
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsWFyDetailBo bo) {
        NLaboratoryMechanicsWFyDetail add = BeanUtil.toBean(bo, NLaboratoryMechanicsWFyDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改万能机详情信息(3标)
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsWFyDetailBo bo) {
        NLaboratoryMechanicsWFyDetail update = BeanUtil.toBean(bo, NLaboratoryMechanicsWFyDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsWFyDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除万能机详情信息(3标)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
